# -- coding: utf-8 --
import sys
import io
# 设置标准输出的编码为UTF-8
if sys.stdout.encoding != 'UTF-8':
    sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
#员工管理系统  -- 重温Python基础
print("=== 员工管理系统 ===")
employees = []
def add_employee(name,department,salary):
  """添加员工"""
  employee = {
    "name":name,
    "department" :department,
    "salary":salary
  }
  employees.append(employee)
  print(f"✅ 已添加员工: {name}")

def show_all_employees():
    """显示所有员工"""
    print("\n--- 所有员工信息 ---")
    for i,emp in enumerate(employees,1):
     print(f"{i}. 姓名：{emp['name']}，部门：{emp['department']},薪资：{emp['salary']}")

def find_employees_by_department(department):
  """按部门查找员工"""
  return [emp for emp in employees if emp["department"] == department]

def calculate_department_salary(department):
  """计算部门总薪资"""
  dept_employees = find_employees_by_department(department)
  total = sum(emp["salary"] for emp in dept_employees)
  return total

def get_highest_paid_employee():
  """获取薪资最高的员工"""
  if not employees:
    return None
  return max(employees,key = lambda emp :emp["salary"])

#测试数据
add_employee("张三", "技术部", 15000)
add_employee("李四", "技术部", 18000)
add_employee("王五", "市场部", 12000)
add_employee("赵六", "市场部", 13000)
add_employee("钱七", "人事部", 10000)
# 显示所有员工
show_all_employees()

#部门统计
# def department_stats():
#   print("\n--- 统计部门 ---")
  
#   depts = {}
#   for emp in  employees:
#     dept = emp['department']
#     if dept not in depts:
#       depts[dept] = []
#     depts[dept].append(emp)

#   for dept, emps in depts.items():
#     total_salary = sum(emp['salary'] for emp in emps)
#     print(f"{dept}: {len(emps)}人,总薪资：{total_salary}元")

# department_stats()
  
print("\n--- 部门统计 ---")
departments = set(emp["department"] for emp in employees)

for dept in departments:
  dept_empployees = find_employees_by_department(dept)
  total_salary = calculate_department_salary(dept)
  avg_salary = total_salary // len(dept_empployees) if dept_empployees else 0
  print(f"{dept}：{len(dept_empployees)}人，总薪资：{total_salary}元，平均薪资：{avg_salary}元")

#最高薪资员工
highest_paid = get_highest_paid_employee()
if highest_paid:
  print(f"\n💰 薪资最高的员工{highest_paid['name']}，薪资：{highest_paid['salary']}元")

print("\n🎉 员工管理系统演示完成！")